package com.story.modular.app.controller;

import com.story.common.persistence.model.PlayCount;
import com.story.common.persistence.model.UseApp;
import com.story.modular.app.dao.PlayCountDao;
import com.story.modular.app.dao.UseAppDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 * @decription app使用量和播放量统计
 * @Date 2018-05-18 10:33
 */
@Controller
public class StatisticsController {

    private static final String PREFIX = "/app/statistics/";

    @Autowired
    private UseAppDao useAppDao;

    @Autowired
    private PlayCountDao playCountDao;

    /**
     * 进入统计页面
     * @return
     */
    @RequestMapping(value = "/statistics")
    public String statisticsPage() {
        return PREFIX + "statistics.html";
    }

    /**
     * app使用量统计
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/statistics/useApp",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Map> useAppStatistics(@RequestParam(value = "beginTime",required = false) String startTime,
                                           @RequestParam(value = "endTime",required = false) String endTime) {
        Map<String,Object> result = new HashMap<>();
        List<UseApp> useAppList = useAppDao.selectUserApp(startTime,endTime);
        List<String> days = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        useAppList.stream().forEach(useApp -> {
            days.add(format.format(useApp.getDay()));
            counts.add(useApp.getCount());
        });
        Collections.reverse(days);
        Collections.reverse(counts);
        result.put("days",days);
        result.put("counts",counts);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频播放量统计
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/statistics/playCount",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Map> playCountStatistics(@RequestParam(value = "beginTime",required = false) String startTime,
                                                @RequestParam(value = "endTime",required = false) String endTime) {
        Map<String,Object> result = new HashMap<>();
        List<PlayCount> playCountList = playCountDao.playCountVedio(startTime,endTime);
        List<String> days = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        playCountList.stream().forEach(playCount -> {
            days.add(format.format(playCount.getDayPlay()));
            counts.add(playCount.getPlayCount());
        });
        Collections.reverse(days);
        Collections.reverse(counts);
        result.put("days",days);
        result.put("counts",counts);
        return ResponseEntity.ok(result);
    }

    /**
     * 得到使用量和播放量的数据
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/statistics/twoDataCount",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Map> twoDataStatistics(@RequestParam(value = "beginTime",required = false) String startTime,
                                                   @RequestParam(value = "endTime",required = false) String endTime) {
        Map<String,Object> result = new HashMap<>();
        List<Map<String,Object>> playCountList = useAppDao.selectTwoDate(startTime,endTime);
        List<String> days = new ArrayList<>();
        List<Integer> useCounts = new ArrayList<>();
        List<Integer> playCounts = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        playCountList.stream().forEach(map -> {
            days.add(format.format((Date)map.get("day")));
            useCounts.add((Integer) map.get("count"));
            playCounts.add((Integer) map.get("playCount"));
        });
        Collections.reverse(days);
        Collections.reverse(useCounts);
        Collections.reverse(playCounts);
        result.put("days",days);
        result.put("useCounts",useCounts);
        result.put("playCounts",playCounts);
        return ResponseEntity.ok(result);
    }
}
